#include "tuxingwidget.h"
#include "ui_tuxingwidget.h"
#include <QPainter>
#include <QBitmap>
#include <QStyleOption>
#include <QtDebug>
#include <QMenu>
#include <QToolButton>
#include <QWidgetAction>
#include <QScrollArea>
#include <QToolButton>


TuXingWidget::TuXingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TuXingWidget)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);// 设置窗口标志，使其表现为弹出窗口，并且无边框


    // 设置menu的样式，后续两个toolBtn要使用
        // 获取widget大小。方便设置toolBtn的大小
    QWidget *widget = ui->widget_2;
    QSize widgetSize = widget->size();
    int width = widgetSize.width();
    int height = widgetSize.height();
    QFont font;
    font.setPointSize(13);

    // 1.设置“轮廓”menu的样式
    QMenu *menu_lunkuo = new QMenu(ui->widget_2);
    QString styleSheet = "QMenu::item { height: 32px; padding: 5px;}"
                         "QMenu::item:selected { background-color: lightblue; border: 1px solid grey;}"
                         "QMenu::icon {height: 32px; }";

    menu_lunkuo->setStyleSheet(styleSheet);
    menu_lunkuo->setFont(font); // 设置字体
    menu_lunkuo->setFont(font); // 设置字体

//    QAction *act_wlkx = new QAction(" 无论廓线", menu_lunkuo);
    QIcon icon1(":/res/无轮廓线.png");
//    act_wlkx->setIcon(icon1);
//    menu_lunkuo->addAction(act_wlkx);

//    QAction *act_wlkx = menu_lunkuo->addAction(" 无轮廓线");
//    QIcon icon1(":/res/无轮廓线.png");
//    act_wlkx->setIcon(icon1);
    QAction *act_cs = menu_lunkuo->addAction(" 纯色");
    QIcon icon2(":/res/纯色.png");
    act_cs->setIcon(icon2);
//    QAction *act_lb = menu_lunkuo->addAction(" 蜡笔");
//    QIcon icon3(":/res/蜡笔.png");
//    act_lb->setIcon(icon3);

    //  1.1 “轮廓”toolBtn
    QToolButton *toolButton = new QToolButton(ui->widget_2);
    toolButton->setMenu(menu_lunkuo); // 绑定toolBtn的Menu
    toolButton->setPopupMode(QToolButton::InstantPopup);
    toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolButton->setIcon(QIcon(":/res/轮廓.png"));
    toolButton->setText("   轮廓");
    toolButton->setGeometry(0,0,width,height);

    //2. 设置“填充”menu
    QMenu *menu_tianchong = new QMenu(ui->widget_3);
    menu_tianchong->setStyleSheet(styleSheet);
    menu_tianchong->setFont(font);

    QAction *act2_btc = menu_tianchong->addAction(" 不填充");
    act2_btc->setIcon(icon1);
    QAction *act2_cs = menu_tianchong->addAction(" 纯色");
    act2_cs->setIcon(icon2);
//    QAction *act2_lb = menu_tianchong->addAction(" 蜡笔");
//    act2_lb->setIcon(icon3);

            //“填充”的两个点击事件
    QObject::connect(act2_btc, &QAction::triggered, [=]() {
        qDebug() << "row79， 不填充被点击";
        emit isFilled(false);

    });

    QObject::connect(act2_cs, &QAction::triggered, [=]() {
        qDebug() << "row84, 纯色被点击";
        emit isFilled(true);
    });


    //  2.1“填充”toolBtn
    QToolButton *toolButton_2 = new QToolButton(ui->widget_3);
    toolButton_2->setMenu(menu_tianchong);
    toolButton_2->setPopupMode(QToolButton::InstantPopup);
    toolButton_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); // 设置图标和文本并排显示
    toolButton_2->setIcon(QIcon(":/res/上色.png")); //图表
    toolButton_2->setText("   填充"); //文本
    toolButton_2->setGeometry(0,0,width,height); // 设置位置和大小



    /*形状选择部分*/
//    QScrollArea *scrollArea = ui->scrollArea;
    QWidget *contentWidget = ui->widget_4;
    QGridLayout *gridLayout = new QGridLayout(contentWidget);
    gridLayout->setSpacing(0);  // 设置组件之间的间距
    gridLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);  // 设置组件的对齐方式
    gridLayout->setContentsMargins(0, 0, 0, 0);
//    scrollArea->setWidget(contentWidget);
//    scrollArea->setWidgetResizable(true);
//    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

        //图形组件部分
            //直线
    QToolButton *bts_zx = new QToolButton();
    bts_zx->setObjectName("bts_zx");
    bts_zx->setIcon(QIcon(":/res/直线.png"));
    bts_zx->setToolTip("直线");  // 设置按钮的工具提示
    bts_zx->setFixedSize(40, 40);
    gridLayout->addWidget(bts_zx, 0, 0);  // 添加到第一行第一列
//    connect(bts_zx, &QToolButton::clicked, this, [this]() {
//        emit changeDrawingMode_zx();
//    });

//            //曲线
//    QToolButton *bts_qx = new QToolButton();
//    bts_qx->setIcon(QIcon(":/res/曲线.png"));
//    bts_qx->setToolTip("曲线");
//    bts_qx->setFixedSize(40, 40);
//    gridLayout->addWidget(bts_qx, 0, 1);  // 添加到第一行第二列

            //椭圆
    QToolButton *bts_tyx = new QToolButton();
    bts_tyx->setObjectName("bts_tyx");
    bts_tyx->setIcon(QIcon(":/res/椭圆.png"));
    bts_tyx->setToolTip("椭圆");  // 设置按钮的工具提示
    gridLayout->addWidget(bts_tyx);
    bts_tyx->setFixedSize(40, 40);
    gridLayout->addWidget(bts_tyx, 0, 2);  // 添加到第一行第三列
//    connect(bts_tyx, &QToolButton::clicked, this, [this]() {
//        emit changeDrawingMode_tyx();
//    });

            //矩形
    QToolButton *bts_jx = new QToolButton();
    bts_jx->setObjectName("bts_jx");
    bts_jx->setIcon(QIcon(":/res/矩形.png"));
    bts_jx->setToolTip("矩形");  // 设置按钮的工具提示
    gridLayout->addWidget(bts_jx);
    bts_jx->setFixedSize(40, 40);
    gridLayout->addWidget(bts_jx, 0, 3);  // 添加到第一行第四列
//    connect(bts_jx, &QToolButton::clicked, this, [this]() {
//        emit changeDrawingMode_jx();
//    });

//            //圆角矩形
//    QToolButton *bts_yjjx = new QToolButton();
//    bts_yjjx->setIcon(QIcon(":/res/圆角矩形.png"));
//    bts_yjjx->setToolTip("矩形");
//    gridLayout->addWidget(bts_yjjx);
//    bts_yjjx->setFixedSize(40, 40);
//    gridLayout->addWidget(bts_yjjx, 1, 0);

            //三角形
    QToolButton *bts_sjx = new QToolButton();
    bts_sjx->setObjectName("bts_sjx");
    bts_sjx->setIcon(QIcon(":/res/三角形.png"));
    bts_sjx->setToolTip("三角形");
    gridLayout->addWidget(bts_sjx);
    bts_sjx->setFixedSize(40, 40);
    gridLayout->addWidget(bts_sjx, 1, 1);
//    connect(bts_sjx, &QToolButton::clicked, this, [this]() {
//        emit changeDrawingMode_sjx();
//    });

            //直角三角形
    QToolButton *bts_zjsjx = new QToolButton();
    bts_zjsjx->setObjectName("bts_zjsjx");
    bts_zjsjx->setIcon(QIcon(":/res/直角三角形.png"));
    bts_zjsjx->setToolTip("直角三角形");
    gridLayout->addWidget(bts_zjsjx);
    bts_zjsjx->setFixedSize(40, 40);
    gridLayout->addWidget(bts_zjsjx, 0, 1);
//    connect(bts_zjsjx, &QToolButton::clicked, this, [this]() {
//        emit changeDrawingMode_zjsjx();
//    });

            //菱形
    QToolButton *bts_lx = new QToolButton();
    bts_lx->setObjectName("bts_lx");
    bts_lx->setIcon(QIcon(":/res/菱形.png"));
    bts_lx->setToolTip("菱形");
    gridLayout->addWidget(bts_lx);
    bts_lx->setFixedSize(40, 40);
    gridLayout->addWidget(bts_lx, 1, 0);
//    connect(bts_lx, &QToolButton::clicked, this, [this]() {
//        emit changeDrawingMode_lx();
//    });

    // 将所有图形toolButton设置为可选
    QList<QToolButton*> allButtons = ui->widget_4->findChildren<QToolButton*>();
    for (QToolButton* button : allButtons) {
        button->setCheckable(true);
        connect(button, &QToolButton::clicked, [this, button, allButtons] () {
//            emit sonOfTuxingIsSelected();
            QString objectName = button->objectName();
            QString drawingMode = "图形";
            QString sonMode;
            if (objectName == "bts_zx") {
                sonMode = "直线";
                emit sonOfTuxingIsSelected(drawingMode, sonMode);
            } else if (objectName == "bts_tyx") {
                emit sonOfTuxingIsSelected(drawingMode, "椭圆");

            } else if (objectName == "bts_jx") {
                emit sonOfTuxingIsSelected(drawingMode, "矩形");

            } else if (objectName == "bts_sjx") {
                emit sonOfTuxingIsSelected(drawingMode, "三角形");

            } else if (objectName == "bts_zjsjx") {
                emit sonOfTuxingIsSelected(drawingMode, "直角三角形");

            } else if (objectName == "bts_lx") {
                emit sonOfTuxingIsSelected(drawingMode, "菱形");
            }
        });
    }

}

TuXingWidget::~TuXingWidget()
{
    delete ui;
}

void TuXingWidget::paintEvent(QPaintEvent *event)
{
    //绘制样式
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式
    QBitmap bmp(this->size());
    bmp.fill();
    QPainter painter(&bmp);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::black);
    painter.setRenderHint(QPainter::Antialiasing);
    //设置边框为圆角12px
    painter.drawRoundedRect(bmp.rect(), 12, 12);
    setMask(bmp);
    //再画颜色块
    QRect tmpRect = QRect(0, 0, this->width(), this->height());
    QBrush brush = QBrush(QColor("#ffffff"));
    p.setPen(Qt::NoPen);  //去掉边框线
    p.setBrush(brush);
    p.drawRect(tmpRect);

}

QList<QToolButton*> TuXingWidget::getTuxingButtons()
{
    QList<QToolButton*> buttonList;
    // 查找 widget_4 下的所有 QToolButton
    QList<QToolButton*> allButtons = ui->widget_4->findChildren<QToolButton*>();
    // 将 QToolButton 添加到列表中
    for (QToolButton* button : allButtons) {
        buttonList.append(button);
    }
    qDebug() << "row 190, num of tuxingButton is " << buttonList.size();

    return buttonList;
}
